package cn.iocoder.boot.learn_200712_springmvc2.learn.controller;

import cn.iocoder.boot.learn_200712_springmvc2.learn.dao.MessageInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    // 存储留言信息
    List<MessageInfo> messageInfos = new ArrayList<>();

    // 获取全部留言信息
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public List<MessageInfo> getList() {
        return messageInfos;
    }

    // 发表新留言
    @RequestMapping(value = "/publish", method = RequestMethod.POST, produces = "application/json")
    public String publish(@RequestBody MessageInfo messageInfo) {
        // 判空
        if (StringUtils.hasLength(messageInfo.getFrom())
        && StringUtils.hasLength(messageInfo.getTo())
        && StringUtils.hasLength(messageInfo.getMessage())) {
            messageInfos.add(messageInfo);
            return "{ \"ok\" : 1 }";
        }
        return "{\"ok\" : 0}";
    }
}
